package com.jx.smartbot.controller;

import com.gocom.imoss.common.Exception.*;
import com.jx.smartbot.util.JacksonUtil;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
//import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

/**
 * Created by fenglitan on 15/5/16.
 */
@ControllerAdvice
public class BaseController extends ResponseEntityExceptionHandler {
    public static final String JSON_UTF_8 = "application/json; charset=UTF-8";

    @ExceptionHandler(value = {AuthorizedException.class})
    public ResponseEntity<?> handlerServerError(AuthorizedException ex, WebRequest request) {
        Response response = new Response();
        response.status = ex.getCode();
        response.msg = ex.getMsg();
        response.body = ex.getErrorObj();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.parseMediaType(JSON_UTF_8));
        return handleExceptionInternal(ex, JacksonUtil.toJSon(response), headers, HttpStatus.UNAUTHORIZED, request);
    }

    @ExceptionHandler(value = {ForbiddenException.class})
    public ResponseEntity<?> handlerServerError(ForbiddenException ex, WebRequest request) {
        Response response = new Response();
        response.status = ex.getCode();
        response.msg = ex.getMsg();
        response.body = ex.getErrorObj();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.parseMediaType(JSON_UTF_8));
        return handleExceptionInternal(ex, JacksonUtil.toJSon(response), headers, HttpStatus.FORBIDDEN, request);
    }

    @ExceptionHandler(value = {NotFoundException.class})
    public ResponseEntity<?> handlerServerError(NotFoundException ex, WebRequest request) {
        Response response = new Response();
        response.status = ex.getCode();
        response.msg = ex.getMsg();
        response.body = ex.getErrorObj();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.parseMediaType(JSON_UTF_8));
        return handleExceptionInternal(ex, JacksonUtil.toJSon(response), headers, HttpStatus.NOT_FOUND, request);
    }

    @ExceptionHandler(value = {ServerException.class})
    public ResponseEntity<?> handlerServerError(ServerException ex, WebRequest request) {
        Response response = new Response();
        response.status = ex.getCode();
        response.msg = ex.getMsg();
        response.body = ex.getErrorObj();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.parseMediaType(JSON_UTF_8));
        return handleExceptionInternal(ex, JacksonUtil.toJSon(response), headers, HttpStatus.INTERNAL_SERVER_ERROR, request);
    }

    @ExceptionHandler(value = {ParamsException.class})
    public ResponseEntity<?> handlerServerError(ParamsException ex, WebRequest request){
        Response response = new Response();
        response.status = ex.getCode();
        response.msg = ex.getMsg();
        response.body = ex.getErrorObj();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.parseMediaType(JSON_UTF_8));
        return handleExceptionInternal(ex, JacksonUtil.toJSon(response), headers, HttpStatus.BAD_REQUEST, request);
    }

    @ExceptionHandler(value = {Exception.class})
    public ResponseEntity<?> handlerServerError(Exception ex, WebRequest request){
        ex.printStackTrace();
        Response response = new Response();
        response.status = 500;
        response.msg = "服务器异常";
        response.body = null;
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.parseMediaType(JSON_UTF_8));
        return handleExceptionInternal(ex, JacksonUtil.toJSon(response), headers, HttpStatus.BAD_REQUEST, request);
    }
    @ExceptionHandler(value = {GoComException.class})
    public ResponseEntity<?> handlerServerError(GoComException ex, WebRequest request){
    	  HttpHeaders headers = new HttpHeaders();
          headers.setContentType(MediaType.parseMediaType(JSON_UTF_8));
          return handleExceptionInternal(ex, ex.getErrString(), headers, HttpStatus.OK, request);
    }

}

